package de.fhl.subWaehlerVerzeichnis;

/**
 * Ueberschrift:  OnlineWahl
 * Copyright:     Copyright (c) 2010 
 * Organisation:  FH Luebeck - Virtuelle FH
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

import org.hibernate.HibernateException;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import de.fhl.entity.Wahlkreise;
import de.fhl.util.HibernateUtil;

/**
 * Die Klasse CreateWaehlerListe generiert eine Wählerliste<br>
 * Sie wurde zum einmaligen erstellen einer Wählerlist geschrieben
 * 
 * @author Achim Krumrein (krumreia@stud.fh-luebeck.de)
 * @author Thomas Weber (webertho@googlemail.de)
 * @version 1.0
 */
public class CreateWaehlerliste {	

	public static void main(String[] args) throws Exception {

		CreateWaehlerliste iwl = new CreateWaehlerliste();
		
		String[] status = new String[2];
		status[0] = "berechtigt";
		status[1] = "nicht berechtigt";

		try {
			int id = 0;
			File file = new File("doc/Waehlerliste.txt");
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);

			StringBuffer sb = new StringBuffer();
			String eachLine = br.readLine();

			while (eachLine != null) {
				
				String loginId = iwl.loginId();
				int wahlkreis = iwl.wahlkreis();
				int st = iwl.berechtigung();
				
				eachLine = eachLine + loginId + "; " + wahlkreis + "; " + status[st];
				
//				writeText(eachLine);
				

				String[] sa = eachLine.split(";");
				System.out.println(eachLine);

				// String[] sa = eachLine.split(";");
				// if (!sa[0].equals("Wahlkreisnummer")) {
				// ++id;
				//
				// ImportWaehlerliste iw = new ImportWaehlerliste();
				// iw.saveWahlkreis(id, sa[1]);
				// }

				sb.append(eachLine);
				sb.append("\n");
				eachLine = br.readLine();
				Thread.sleep(100);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	private static void writeText(String eachLine) throws IOException {
		FileWriter fw = new FileWriter("doc/Waehlerliste_komplett.txt");
		BufferedWriter bw = new BufferedWriter(fw);
		
		try {
			bw.write(eachLine);
			bw.newLine();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			fw.close();
		}
	}

	public void saveWahlkreis(int id, String wahlkreis) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {

			transaction = session.beginTransaction();
			Wahlkreise wk = new Wahlkreise();
			wk.setWahlkreisNr(id);
			wk.setWahlkreisName(wahlkreis);
			session.persist(wk);
			transaction.commit();

		} catch (HibernateException e) {

			transaction.rollback();
			e.printStackTrace();

		} finally {
			session.close();
		}
	}

	private String loginId() {
		String allowedChars = "0123456789abcdefghklmnopqrstuvwxyzABCDEFGHKLMNOPQRSTUVWXYZ";
		Random random = new Random();
		String randomString = generateRandomString(allowedChars, random);
		return randomString;
	}

	private static String generateRandomString(String allowedChars,
			Random random) {
		int max = allowedChars.length();
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < 8; i++) {
			int value = random.nextInt(max);
			buffer.append(allowedChars.charAt(value));
		}
		return buffer.toString();
	}
	
	private int wahlkreis(){
		int[] allowedWk = {1, 13, 20, 29, 31, 40, 47, 60, 92, 111};
		int max = allowedWk.length;
		int wk = 0;
		Random random = new Random();
		wk = allowedWk[random.nextInt(max)];
		return wk;
	}
	
	private int berechtigung(){
		int b;
		Random random = new Random();
		b = random.nextInt(2);
		return b;
	}
}
